package models.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import models.repo.IRepository;

import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import models.entities.Staff;
import models.enumtype.ActiveType;
import models.enumtype.KPaging;
import models.enumtype.StaffRoleType;
import models.vo.CustomerSaleVO;
import models.vo.NVBHSaleVO;
import models.vo.StaffPositionVO;
import models.vo.SupervisorSaleVO;
import models.vo.TreeVO;
import utils.StringUtil;
import utils.Unicode2English;

@Service
public class StaffDAO {

	@Autowired
	private IRepository repo;

	public Staff createStaff(Staff staff) throws Exception {
		if (staff == null) {
			throw new IllegalArgumentException("staff");
		}
		repo.create(staff);
		return repo.getEntityById(Staff.class, staff.getStaffId());
	}

	public void deleteStaff(Staff staff) throws Exception {
		if (staff == null) {
			throw new IllegalArgumentException("staff");
		}
		repo.delete(staff);
	}

	public Staff getStaffById(long id) throws Exception {
		return repo.getEntityById(Staff.class, id);
	}

	public void updateStaff(Staff staff) throws Exception {
		if (staff == null) {
			throw new IllegalArgumentException("staff");
		}
		repo.update(staff);
	}

	public Staff getStaffByCode(String code) throws Exception {
		code = code.toUpperCase();
		String sql = "select * from STAFF where upper(staff_code)=?";
		List<Object> params = new ArrayList<Object>();
		params.add(code.toUpperCase());
		Staff rs = repo.getEntityBySQL(Staff.class, sql, params);
		return rs;
	}
	
	public List<Staff> getListStaff(KPaging<Staff> kPaging, String staffCode, String staffName, ActiveType status) throws Exception {
		StringBuilder sql = new StringBuilder();
		List<Object> params = new ArrayList<Object>();
		sql.append("select * from staff where 1 = 1 ");
		if(!StringUtil.isNullOrEmpty(staffCode)) {
			sql.append(" staff_code = ?");
			params.add(staffCode);
		}
		if(!StringUtil.isNullOrEmpty(staffName)) {
			sql.append(" NAME_TEXT like ? ESCAPE '/' ");
			params.add(StringUtil.toOracleSearchLike(Unicode2English.codau2khongdau(staffName)));
		}
		if(status != null) {
			sql.append(" status = ?");
			params.add(status.getValue());
		}
		if(kPaging != null) {
			return repo.getListBySQLPaginated(Staff.class, sql.toString(), params, kPaging);
		} else {
			return repo.getListBySQL(Staff.class, sql.toString(), params);
		}
	}
}
